﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Entities;
using Context;
namespace WCF_ListenMusic.Services.Module_Musician.Public
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MusicianService" in code, svc and config file together.
    public class MusicianService : IMusicianService
    {
        Context.Connect_DBDataContext context = new Connect_DBDataContext();
        public List<Musician> getAllMusician()
        {
            try {
                var q = from a in context.Musicians
                        select a;
                return q.ToList();
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }


        public int getMaxID_Musician()
        {
            context.CommandTimeout = 3600;
            try
            {
                var q = (from a in context.Musicians
                        select a.MusicianID).Max();
                return q;
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }

        public Musician checkMusicianName(string name)
        {
            context.CommandTimeout = 3600;
            try
            {
                var q = from a in context.Musicians
                        where a.MusicianName.Trim().ToLower().Equals(name.Trim().ToLower())
                        select a;
                return q.SingleOrDefault();
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }

        [OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]
        public bool insertNewMusician(Musician musician)
        {
            context.CommandTimeout = 3600;
            try
            {
                musician.MusicianID = getMaxID_Musician() + 1;
                context.Musicians.InsertOnSubmit(musician);
                context.SubmitChanges();
                return true;
            }
            catch (Exception e) { return false; throw new FaultException(e.Message); }
        }


        public Musician getMusician_byID(int musicianid)
        {
            try
            {
                var q = from a in context.Musicians
                        where a.MusicianID.Equals(musicianid)
                        select a;
                return q.SingleOrDefault();
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }
    }
}
